package net.tmaize.dao.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import net.tmaize.dao.HotDao;
import net.tmaize.entity.List_hot;
import net.tmaize.entity.News_hot;
import net.tmaize.utils.ApiUtils;
import net.tmaize.utils.JsonUtils;
import net.tmaize.utils.UrlUtils;

public class HotDaoImpl implements HotDao {

	private String jsonStr = null;
	
	@Override
	public News_hot getHot() throws JSONException, IOException {
		
		jsonStr = JsonUtils.getJsonStr(ApiUtils.hotURL);
		News_hot hot = new News_hot();
		List<List_hot> recent = new ArrayList<List_hot>();

		JSONTokener jsonTokener = new JSONTokener(jsonStr);
		JSONObject jsonObject = new JSONObject(jsonTokener);
		JSONArray jsonArray = jsonObject.getJSONArray("recent");

		for (int i = 0; i < jsonArray.length(); i++) {
			JSONObject jb = jsonArray.getJSONObject(i);
			String news_id = jb.getString("news_id");
			String url = jb.getString("url");
			String thumbnail = jb.getString("thumbnail");
			String title = jb.getString("title");
			recent.add(new List_hot(news_id, url, UrlUtils.imageUrlChange_1(thumbnail), title));
		}
		hot.setRecent(recent);
		return hot;
	}
}
